<?php
	$process_type = "edit";	// Please choose only ONE !! Edit / Preview / Delete . 'Edit' and 'Add' are considered as the same value.
	$process_module_name = "moduleprivilege";	// Module Name
	$process_module_id = "3";	// Module ID
	$process_module_sub_id = "0";	// Module Sub ID
	$process_module_category = "management"; // Module Category
	//$rowDivWidth = 60;	// Width for first column label form (pixel)
	$form_name = "form_moduleprivilege";	// Form Name
	$form_action = "proc_moduleprivilege.php";	// Form Action
?>
<div class="content">
	<form action="process/<?php echo $form_action; ?>" method="post" name="<?php echo $form_name; ?>" id="moduleprivilege" style="margin:0; padding:0;">
	<?php include("action/act_includes_global/1_act_inc_global_formstartconfig.php"); // Do not Edit !!! ?>
	<?php include("action/act_includes_global/3_act_inc_global_formrowdatahidden.php"); // Do not Edit !!! Every form must include this file !  ?>

	<div class="content_header_left">
		<!-- Start Content Header Left -->
		<?php include("ws_includes/1_ws_inc_priviligestring.php"); ?>
		<?php include("ws_includes/2_ws_inc_headerlabel.php"); ?>
		<!-- End Content Header Left -->
	</div>
	<div class="content_header_right">
		<!-- Start Content Header Right -->
		<?php if($privilegeView == 1 && $privilegeAdd == 1 && $privilegeEdit == 1) { ?>
		<input id="cancel" type="button" onClick="history.go(0)" style="height:24px; visibility: hidden;" value="Cancel" />
		<button id="submit" type="submit" style="height:24px;" >Submit</button>
		<?php } ?>
		<!-- End Content Header Right -->
	</div>
	<div style="clear:both;"></div>
	<div class="content_body">
		<!-- Start Content Body -->
		<?php if($privilegeView == 1) { ?>
			
			<?php
				$sql_ug = "	select usergroupid, usergroupname, usergroupstatus
							from ms_user_group 
							where 	usergroupid >= ".$_SESSION['usergroupid']." 
								and stsrc = 'A'
						";
				$exe_ug = mysql_query($sql_ug) or die(mysql_error());
				$num_ug = mysql_num_rows($exe_ug);
				
				if($num_ug > 0)
				{
			?>
			<div>
				<?php
						//$module_count = 1;
						$number_count = 1;
						while($row_ug = mysql_fetch_array($exe_ug))
						{
							$module_count = $row_ug['usergroupid'];
							$disabled = false;
							if($module_count <= $_SESSION['usergroupid'] || $privilegeAdd != 1 || $privilegeEdit != 1){$disabled = true;}
						
				?>			
							<span style="<?php if($row_ug['usergroupstatus'] != 1){echo "color:red;";} ?>"><?php echo $number_count.". ".ucwords(strtolower($row_ug[1])); ?></span>
							<div class="divExpand">
								<div class="collapse" style="margin:0; padding:0;">
									
							
							<?php
								$mg = array("module" => 1, "management" => 2);
								
								//$mg_count = 1;
								
								foreach($mg as $mg => $mg_val)
								{
									$mg_count = $mg_val;
							?>
								<!--<div style="padding-left:30px;"><?php //echo ucwords(strtolower($mg)); ?> :</div>-->
								<?php
									if($mg == "module")
									{
										$sql = "
											select distinct m.*, n.mgp_enable, n.mgp_insert, n.mgp_update, n.mgp_delete
											from 
											(
												(
													select distinct a.moduleid as id, a.modulename as name, a.modulelabel as label, 'module' as type, 
																	b.modulegroupprivilegeenable, b.modulegroupprivilegeinsert, b.modulegroupprivilegeupdate, b.modulegroupprivilegedelete, 
																	c.usergroupid as groupid,
																	a.modulestatus as status
													from ms_module a 
														left join ms_module_group_privilege b 
															on a.moduleid = b.modulegroupprivilegemoduleid 
																and b.stsrc = 'A' 
																and b.modulegroupprivilegetype = 'module' 
																and b.usergroupid = ".$row_ug[0]." 
																and b.modulegroupprivilegemodulesubid = 0 
														left join ms_user_group c 
															on c.usergroupid = ".$row_ug[0]." 
																and c.stsrc = 'A' 
													where a.stsrc = 'A'
												)
											) m 
												left join
												(
													select distinct a.moduleid as id, a.modulename as name, a.modulelabel as label, 'module' as type, 
																	b.modulegroupprivilegeenable as mgp_enable, b.modulegroupprivilegeinsert as mgp_insert, b.modulegroupprivilegeupdate as mgp_update, b.modulegroupprivilegedelete as mgp_delete, 
																	c.usergroupid as groupid,
																	a.modulestatus as status
													from ms_module a 
														left join ms_module_group_privilege b 
															on a.moduleid = b.modulegroupprivilegemoduleid 
																and b.stsrc = 'A' 
																and b.modulegroupprivilegetype = 'module' 
																and b.usergroupid = ".($row_ug[0]-1)." 
																and b.modulegroupprivilegemodulesubid = 0 
														left join ms_user_group c 
															on c.usergroupid = ".($row_ug[0]-1)." 
																and c.stsrc = 'A' 
													where a.stsrc = 'A'
												) n
													on 		m.id = n.id
														and m.name = n.name
														and m.label = n.label
														and m.type = n.type
														and m.status = n.status
											where m.type = '".$mg."' order by m.id asc
										";
									}
									else if($mg == "management")
									{
										$sql = "
											select distinct m.*, n.mgp_enable, n.mgp_insert, n.mgp_update, n.mgp_delete
											from 
											(
												(
													select distinct 	a.moduleadminid as id, a.moduleadminname as name, a.moduleadminlabel as label, 'management' as type, 
																		b.modulegroupprivilegeenable, b.modulegroupprivilegeinsert, b.modulegroupprivilegeupdate, b.modulegroupprivilegedelete, 
																		c.usergroupid as groupid,
																		'1' as status
													from ms_module_admin a 
														left join ms_module_group_privilege b 
															on a.moduleadminid = b.modulegroupprivilegemoduleid 
																and b.stsrc = 'A' 
																and b.modulegroupprivilegetype = 'management' 
																and b.usergroupid = ".$row_ug[0]." 
																and b.modulegroupprivilegemodulesubid = 0 
														left join ms_user_group c 
															on c.usergroupid = ".$row_ug[0]." 
																and c.stsrc = 'A' 
													where a.stsrc = 'A'
												)
											) m 
												left join
												(
													select distinct 	a.moduleadminid as id, a.moduleadminname as name, a.moduleadminlabel as label, 'management' as type, 
																		b.modulegroupprivilegeenable as mgp_enable, b.modulegroupprivilegeinsert as mgp_insert, b.modulegroupprivilegeupdate as mgp_update, b.modulegroupprivilegedelete as mgp_delete, 
																		c.usergroupid as groupid,
																		'1' as status
													from ms_module_admin a 
														left join ms_module_group_privilege b 
															on a.moduleadminid = b.modulegroupprivilegemoduleid 
																and b.stsrc = 'A' 
																and b.modulegroupprivilegetype = 'management' 
																and b.usergroupid = ".($row_ug[0]-1)." 
																and b.modulegroupprivilegemodulesubid = 0 
														left join ms_user_group c 
															on c.usergroupid = ".($row_ug[0]-1)." 
																and c.stsrc = 'A' 
													where a.stsrc = 'A'
												) n
													on 		m.id = n.id
														and m.name = n.name
														and m.label = n.label
														and m.type = n.type
														and m.status = n.status
											where m.type = '".$mg."' order by m.id asc
										";
									}
									// echo $sql;die();
									$exe = mysql_query($sql) or die(mysql_error());
									$num = mysql_num_rows($exe);
									
									
								?>
									<div style="padding-left:15px; padding-top:5px; ">
										<table border="0" cellpadding="0" cellspacing="0" class="mod_pvlg_parent_tbl" style="<?php if($mg == "module"){echo "background-color:#9999ff;";}else if($mg == "management"){echo "background-color:#ff9999;";} ?>">
										<tr style="height:20px; vertical-align:middle;">
											<th class="mod_pvlg_parent_td_1"><?php echo ucwords(strtolower($mg)); ?></td>
											<th class="mod_pvlg_parent_td_2">Select All</td>
											<th class="mod_pvlg_parent_td_3">Visible</td>
											<th class="mod_pvlg_parent_td_4">Add</td>
											<th class="mod_pvlg_parent_td_5">Edit</td>
											<th class="mod_pvlg_parent_td_6">Delete</td>
										</tr>
								<?php
									$parent_count = 1;
									
									while($row = mysql_fetch_array($exe))
									{
										$parent_count = $row['id'];
									
										$count_p = 1;
										
										$form_name_parent = $module_count."-".$mg_count."-".$parent_count."-0";
								?>
										<tr style="height:20px; vertical-align:middle;">
											<td class="mod_pvlg_parent_td_1">
												<span style="padding-left:15px;"><?php echo ucwords(strtolower($row[2])); ?></span>
												<?php if($row['status'] != 1){echo "<span style='color:red;'>&bull; OFF</span>";} ?>
												<?php if(1==0){ // Start Debug Test ?>
												<textarea style="width:100px; height:20px; overflow:hidden;" ><?php echo $row[4] ."-".$row[5] ."-".$row[6] ."-". $row[7]; ?></textarea>
												<?php } // End Debug Test ?>
											</td>
											<td class="mod_pvlg_parent_td_2">
												<input type="checkbox" id="selectAllParent_<?php echo $form_name_parent; ?>" <?php if(($row[4] == 1 && $row[5] == 1 && $row[6] == 1 && $row[7] == 1) || ($row_ug[0] == 1)){echo "checked='checked'";} if($disabled == true || (($row['mgp_enable'] == 0 || $row['mgp_insert'] == 0 ||$row['mgp_update'] == 0 ||$row['mgp_delete'] == 0) && $row_ug[0] > 2 && $_SESSION['usergroupid'] != 1)){echo "disabled='disabled'";} ?> onclick="checkedAll('selectAllParent_<?php echo $form_name_parent; ?>','4');" /><?php //SelectAll ?>
											</td>
											<td class="mod_pvlg_parent_td_3">
												<input type="hidden" name="<?php echo $form_name_parent."-V"; ?>" value="0">
												<input type="checkbox" id="selectAllParent_<?php echo $form_name_parent; ?>_<?php echo $count_p; ?>" name="<?php echo $form_name_parent."-V"; ?>" value="1" <?php if($row[4] == 1 || ($row_ug[0] == 1)){echo "checked='checked'";} if($disabled == true || ($row['mgp_enable'] == 0 && $row_ug[0] > 2 && $_SESSION['usergroupid'] != 1)){echo "disabled='disabled'";} ?> onclick="checkuncheckButtonCheckAll('selectAllParent_<?php echo $form_name_parent; ?>','4');" /><?php //Visible ?>
												<?php $count_p++; ?>
											</td>
											<td class="mod_pvlg_parent_td_4">
												<input type="hidden" name="<?php echo $form_name_parent."-A"; ?>" value="0">
												<input type="checkbox" id="selectAllParent_<?php echo $form_name_parent; ?>_<?php echo $count_p; ?>" name="<?php echo $form_name_parent."-A"; ?>" value="1" <?php if($row[5] == 1 || ($row_ug[0] == 1)){echo "checked='checked'";} if($disabled == true || ($row['mgp_insert'] == 0 && $row_ug[0] > 2 && $_SESSION['usergroupid'] != 1)){echo "disabled='disabled'";} ?> onclick="checkuncheckButtonCheckAll('selectAllParent_<?php echo $form_name_parent; ?>','4');" /><?php //Add ?>
												<?php $count_p++; ?>
											</td>
											<td class="mod_pvlg_parent_td_5">
												<input type="hidden" name="<?php echo $form_name_parent."-E"; ?>" value="0">
												<input type="checkbox" id="selectAllParent_<?php echo $form_name_parent; ?>_<?php echo $count_p; ?>" name="<?php echo $form_name_parent."-E"; ?>" value="1" <?php if($row[6] == 1 || ($row_ug[0] == 1)){echo "checked='checked'";} if($disabled == true || ($row['mgp_update'] == 0 && $row_ug[0] > 2 && $_SESSION['usergroupid'] != 1)){echo "disabled='disabled'";} ?> onclick="checkuncheckButtonCheckAll('selectAllParent_<?php echo $form_name_parent; ?>','4');" /><?php //Edit ?>
												<?php $count_p++; ?>
											</td>
											<td class="mod_pvlg_parent_td_6">
												<input type="hidden" name="<?php echo $form_name_parent."-D"; ?>" value="0">
												<input type="checkbox" id="selectAllParent_<?php echo $form_name_parent; ?>_<?php echo $count_p; ?>" name="<?php echo $form_name_parent."-D"; ?>" value="1" <?php if($row[7] == 1 || ($row_ug[0] == 1)){echo "checked='checked'";} if($disabled == true || ($row['mgp_delete'] == 0 && $row_ug[0] > 2 && $_SESSION['usergroupid'] != 1)){echo "disabled='disabled'";} ?> onclick="checkuncheckButtonCheckAll('selectAllParent_<?php echo $form_name_parent; ?>','4');" /><?php //Delete ?>
												<?php $count_p++; ?>
											</td>
										</tr>
										
											<?php
												$sql_sub = "
													select distinct m.*, n.mgp_enable, n.mgp_insert, n.mgp_update, n.mgp_delete
													from 
													(
														(
															select  
																a.modulesubid as id, a.modulesubname as name, a.modulesublabel as label, a.modulesuburltype as type, 
																b.modulegroupprivilegeenable, b.modulegroupprivilegeinsert, b.modulegroupprivilegeupdate, b.modulegroupprivilegedelete,
																a.modulesubstatus as status
															from ms_module_sub a 
																left join ms_module_group_privilege b 
																	on 		b.stsrc = 'A' 
																		and a.modulesuburltype = b.modulegroupprivilegetype 
																		and b.modulegroupprivilegetype = '".$mg."' 
																		and b.modulegroupprivilegemodulesubid <> 0 
																		and a.modulesubid = b.modulegroupprivilegemodulesubid
																		and a.moduleid = b.modulegroupprivilegemoduleid
																		and b.usergroupid = ".$row["groupid"]." 
																left join ms_user_group c 
																	on 		c.stsrc = 'A' 
																		and b.usergroupid = c.usergroupid
																		and c.usergroupid = ".$row["groupid"]." 
															where 	a.stsrc = 'A' 
																and a.moduleid = ".$parent_count."
																and a.modulesuburltype = '".$mg."'
														)
													) m
														left join
														(
															select  
																a.modulesubid as id, a.modulesubname as name, a.modulesublabel as label, a.modulesuburltype as type, 
																b.modulegroupprivilegeenable as mgp_enable , b.modulegroupprivilegeinsert as mgp_insert, b.modulegroupprivilegeupdate as mgp_update, b.modulegroupprivilegedelete as mgp_delete,
																a.modulesubstatus as status
															from ms_module_sub a 
																left join ms_module_group_privilege b 
																	on 		b.stsrc = 'A' 
																		and a.modulesuburltype = b.modulegroupprivilegetype 
																		and b.modulegroupprivilegetype = '".$mg."' 
																		and b.modulegroupprivilegemodulesubid <> 0 
																		and a.modulesubid = b.modulegroupprivilegemodulesubid
																		and a.moduleid = b.modulegroupprivilegemoduleid
																		and b.usergroupid = ".($row["groupid"]-1)." 
																left join ms_user_group c 
																	on 		c.stsrc = 'A' 
																		and b.usergroupid = c.usergroupid
																		and c.usergroupid = ".($row["groupid"]-1)." 
															where 	a.stsrc = 'A' 
																and a.moduleid = ".$parent_count."
																and a.modulesuburltype = '".$mg."'
														) n
															on 		m.id = n.id
																and m.name = n.name
																and m.label = n.label
																and m.type = n.type
																and m.status = n.status
													where m.type = '".$mg."' order by m.id asc
												";
												//echo $sql_sub;die();
												$exe_sub = mysql_query($sql_sub) or die(mysql_error());
												$num_sub = mysql_num_rows($exe_sub);
												
												//$count_row = 1;
												
												while($row_sub = mysql_fetch_array($exe_sub))
												{
												
													$count_row = $row_sub['id'];
												
													$count = 1;
													
													$form_name = $module_count."-".$mg_count."-".$parent_count."-".$count_row;
											?>
													
													<tr style="height:20px; vertical-align:middle;">
														<td class="mod_pvlg_parent_td_1">
															<span style="padding-left:30px;">- <?php echo ucwords(strtolower($row_sub[2])); ?></span>
															<?php if($row_sub['status'] != 1){echo "<span style='color:red;'>&bull; OFF</span>";} ?>
															<?php if(1==0){ // Start Debug Test ?>
															<textarea style="width:100px; height:20px; overflow:hidden;" ><?php echo $sql_sub; ?></textarea>
															<textarea style="width:100px; height:20px; overflow:hidden;" ><?php echo $row_sub[4] ."-".$row_sub[5] ."-".$row_sub[6] ."-". $row_sub[7]; ?></textarea>
															<?php } // End Debug Test ?>
														</td>
														<td class="mod_pvlg_parent_td_2">
															<input type="checkbox" id="selectAllChild_<?php echo $form_name; ?>" <?php if(($row_sub[4] == 1 && $row_sub[5] == 1 && $row_sub[6] == 1 && $row_sub[7] == 1) || ($row_ug[0] == 1)){echo "checked='checked'";} if($disabled == true || (($row_sub['mgp_enable'] == 0 || $row_sub['mgp_insert'] == 0 ||$row_sub['mgp_update'] == 0 ||$row_sub['mgp_delete'] == 0) && $row_ug[0] > 2 && $_SESSION['usergroupid'] != 1)){echo "disabled='disabled'";} ?> onclick="checkedAll('selectAllChild_<?php echo $form_name; ?>','4');" /><?php //Select All ?>
														</td>
														<td class="mod_pvlg_parent_td_3">
															<input type="hidden" name="<?php echo $form_name."-V"; ?>" value="0">
															<input type="checkbox" id="selectAllChild_<?php echo $form_name; ?>_<?php echo $count; ?>"  name="<?php echo $form_name."-V"; ?>" value="1" <?php if($row_sub[4] == 1 || ($row_ug[0] == 1)){echo "checked='checked'";} if($disabled == true || ($row_sub['mgp_enable'] == 0 && $row_ug[0] > 2 && $_SESSION['usergroupid'] != 1)){echo "disabled='disabled'";} ?> onclick="checkuncheckButtonCheckAll('selectAllChild_<?php echo $form_name; ?>','4');" /><?php //Visible ?>
															<?php $count++; ?>
														</td>
														<td class="mod_pvlg_parent_td_4">
															<input type="hidden" name="<?php echo $form_name."-A"; ?>" value="0">
															<input type="checkbox" id="selectAllChild_<?php echo $form_name; ?>_<?php echo $count; ?>" name="<?php echo $form_name."-A"; ?>" value="1" <?php if($row_sub[5] == 1 || ($row_ug[0] == 1)){echo "checked='checked'";} if($disabled == true || ($row_sub['mgp_insert'] == 0 && $row_ug[0] > 2 && $_SESSION['usergroupid'] != 1)){echo "disabled='disabled'";} ?> onclick="checkuncheckButtonCheckAll('selectAllChild_<?php echo $form_name; ?>','4');" /><?php //Add ?>
															<?php $count++; ?>
														</td>
														<td class="mod_pvlg_parent_td_5">
															<input type="hidden" name="<?php echo $form_name."-E"; ?>" value="0">
															<input type="checkbox" id="selectAllChild_<?php echo $form_name; ?>_<?php echo $count; ?>" name="<?php echo $form_name."-E"; ?>" value="1" <?php if($row_sub[6] == 1 || ($row_ug[0] == 1)){echo "checked='checked'";} if($disabled == true || ($row_sub['mgp_update'] == 0 && $row_ug[0] > 2 && $_SESSION['usergroupid'] != 1)){echo "disabled='disabled'";} ?> onclick="checkuncheckButtonCheckAll('selectAllChild_<?php echo $form_name; ?>','4');" /><?php //Edit ?>
															<?php $count++; ?>
														</td>
														<td class="mod_pvlg_parent_td_6">
															<input type="hidden" name="<?php echo $form_name."-D"; ?>" value="0">
															<input type="checkbox" id="selectAllChild_<?php echo $form_name; ?>_<?php echo $count; ?>" name="<?php echo $form_name."-D"; ?>" value="1" <?php if($row_sub[7] == 1 || ($row_ug[0] == 1)){echo "checked='checked'";} if($disabled == true || ($row_sub['mgp_delete'] == 0 && $row_ug[0] > 2 && $_SESSION['usergroupid'] != 1)){echo "disabled='disabled'";} ?> onclick="checkuncheckButtonCheckAll('selectAllChild_<?php echo $form_name; ?>','4');" /><?php //Delete ?>
															<?php $count++; ?>
														</td>
													</tr>
													
											<?php
													//$count_row++;
												}
											?>
								<?php
										//$parent_count++;
									}
								?>
										</table>
									</div>
									
							<?php
							
									//$mg_count++;
								}
							?>
							
									<br />
									
								</div>
							</div>
				<?php
							//$module_count++;
							$number_count++;
						}
					}
					else
					{
				?>
						No data !
				<?php
					}
				?>
			</div>
		<?php } ?>
		<!-- End Content Body -->
	</div>
	<div style="clear:both;"></div>
	</form>
</div>
<!--        Script by hscripts.com          -->
<!--        copyright of HIOX INDIA         -->
<!-- Free javascripts @ http://www.hscripts.com -->
<script type="text/javascript">
	//document.getElementById("cancel").style.visibility = "hidden";
	//document.getElementById("submit").style.visibility = "hidden";

	function checkuncheckButtonCheckAll(IdNameRow,CheckCountRow)
	{
		var aa = document.getElementById(IdNameRow);
		
		/*
		for (var i =1; i <= CheckCountRow; i++) 
		{
			var bb_[i] = document.getElementById((IdNameRow + "_" + i));
		}
		*/
		
		var bb_1 = document.getElementById((IdNameRow + "_1"));
		var bb_2 = document.getElementById((IdNameRow + "_2"));
		var bb_3 = document.getElementById((IdNameRow + "_3"));
		var bb_4 = document.getElementById((IdNameRow + "_4"));
		
		
		if(bb_1.checked == true && bb_2.checked == true && bb_3.checked == true && bb_4.checked == true)
		//if(bb_[1].checked == true && bb_[2].checked == true && bb_[3].checked == true && bb_[4].checked == true)
		{
			aa.checked = true ;
		}
		else
		{
			aa.checked = false ;
		}
		
		document.getElementById("cancel").style.visibility = "visible";
		//document.getElementById("submit").style.visibility = "visible";
	}
	
	function checkedAll (IdName,CheckCount) 
	{
		checked=false;
		var aa = document.getElementById(IdName);
		
		if(aa.checked == checked)
		{
			checked = false;
		}
		else if(aa.checked != checked)
		{
			checked = true;
		}		
		
		for (var i =1; i <= CheckCount; i++) 
		{
			var bb = document.getElementById((IdName + "_" + i));
			bb.checked = checked ;
		}
		
		document.getElementById("cancel").style.visibility = "visible";
		//document.getElementById("submit").style.visibility = "visible";
	}
	
</script>
<!-- Script by hscripts.com -->